package com.shd.edanpei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.EditText;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.presenter.contract.NewCustomerContract;
import com.shd.edanpei.image.LoadImageCtrl;
import com.shd.edanpei.presenter.NewCustomerPresenter;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by mc on 2018/9/19.
 * 新建客户
 */

public class NewCustomerActivity extends BaseActivity<NewCustomerPresenter>
        implements NewCustomerContract.NewCustomerIView {

    private final int PHOTO = 1;

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.tv_headline_right)
    TextView tvRight;
    @BindView(R.id.iv_head)
    CircleImageView ivHead;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_gender)
    TextView tvGender;
    @BindView(R.id.et_phone)
    EditText etPhone;

    @Override
    protected int setLayout() {
        return R.layout.activity_newcustomer;
    }

    @Override
    protected NewCustomerPresenter bindPresenter() {
        return new NewCustomerPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().send(RxBusCode.HOME_CREATE_DIALOG_DISMISS);
        loadImageCtrl = new LoadImageCtrl(this);
        headline.setText("新建客户");
//        tvRight.setText("通讯录");
//        tvRight.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.iv_headline_back)
    void close() {
        finish();
    }

    @OnClick(R.id.tv_headline_right)
    void clickRight() {
        startActivity(ContactsActivity.class);
    }

    @OnClick(R.id.rl_photo)
    void clickPhoto() {
        ChoosePicActivity.start(this, PHOTO);
    }

    @OnClick(R.id.ll_gender)
    void clickGender() {
        DialogUtil.listDialog(this, "性别", R.array.gender, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                tvGender.setText(data);
                tvGender.setTextColor(getResources().getColor(R.color.black));
            }
        });
    }

    @OnClick(R.id.tv_create)
    void create() {
        mPresenter.submit(etName.getText().toString(),
                tvGender.getText().toString(),
                etPhone.getText().toString());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ChoosePicActivity.INTENT_REQUEST_CODE) {
            if (null != data && resultCode == PHOTO) {
                String picturePath = data.getStringExtra(ChoosePicActivity.INTENT_BACK_DATA);
                loadImageCtrl.getGlide().load(picturePath).into(ivHead);
                mPresenter.uploadPicture(picturePath);
            }
        }
    }
}
